home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 301-325 / disk_319 / cnewssrc / uupc.lzh / uupc / serialio.c < prev    next >
C/C++ Source or Header  |  1990-01-16  |  9KB  |  338 lines

  1. /*
  2.  *    SerialIO.c
  3.  *    Created: June 1986 by J.A. Lydiatt
  4.  *
  5.  *    Amiga Serial I/O Library
  6.  *
  7.  *    Note:  This set of routines will not work with Lattice 4.0
  8.  *        since the Lattice stub for SER_SETPARAM is incorrect.
  9.  *        It does work with Manx, however.
  10.  *    Note:
  11.  *      A Task may only open the serial port once.
  12.  *
  13.  *    $Id: serialio.c,v 1.2 90/01/16 10:27:19 crash Exp Locker: crash $
  14.  */
  15.  
  16. #ifndef lint
  17. static char RCSid[] = "$Id: serialio.c,v 1.2 90/01/16 10:27:19 crash Exp Locker: crash $";
  18. #endif /* lint */
  19.  
  20. #include <exec/types.h>
  21. #include <exec/memory.h>
  22. #include <stdio.h>
  23. #include <devices/serial.h>
  24.  
  25. #ifdef MCH_AMIGA
  26. # include <functions.h>        /* Manx */
  27. #else
  28. # include <proto/exec.h>    /* Lattice */
  29. #endif
  30.  
  31. /* Allowable Mode Values */
  32. #define MODEHALF    0
  33. #define MODEFULL    1
  34. #define MODEECHO    2
  35.  
  36. /* Default starting values */
  37. #define STARTBAUD   2400          /* default baud rate */
  38. #define STARTMODE   MODEFULL
  39. #define SERFLAGS    (SERF_SHARED | SERF_XDISABLED | SERF_QUEUEDBRK)
  40. #define CTLCHAR     0x11130501    
  41.  
  42. static int Mode = STARTMODE;
  43. static int amClosing = FALSE;
  44.  
  45. /* declarations for the serial stuff */
  46. typedef struct IORequest * IO_CAST;
  47.  
  48. static struct IOExtSer *Read_Request = NULL;
  49. static UBYTE rs_in[2];
  50. static struct IOExtSer *Write_Request = NULL;
  51. static UBYTE rs_out[2];
  52.  
  53. /* stack to save Serial Port flags */
  54. #define MAXSTACK 10
  55. static int stackSize = 0;
  56. static int   modeStack[ MAXSTACK ];
  57. static UBYTE flagStack[ MAXSTACK ];
  58.  
  59.  
  60. /*------------------------------------------------------------*/
  61. /*  GetSerialSigBit: return Read_Request's Signal Bit          */
  62. /*------------------------------------------------------------*/
  63.  
  64. int GetSerialSigBit()
  65. {
  66.    return Read_Request->IOSer.io_Message.mn_ReplyPort->mp_SigBit;
  67. }
  68.  
  69. /*------------------------------------------------------------*/
  70. /*  CheckSerIO : return TRUE if serial port has a character   */
  71. /*------------------------------------------------------------*/
  72.  
  73. BOOL CheckSerIO()
  74. {
  75.    return (BOOL)( CheckIO( (IO_CAST) Read_Request ) != NULL);
  76. }
  77.  
  78. /*------------------------------------------------------------*/
  79. /*      FlushSerIO: Flush the receive buffer                        */
  80. /*------------------------------------------------------------*/
  81.  
  82. void FlushSerIO()
  83. {
  84.    register struct IOExtSer *r = Read_Request;
  85.  
  86.    AbortIO( (IO_CAST) r );                    /* abort it */
  87.    AbortIO( (IO_CAST) Write_Request );
  88.  
  89.    r->IOSer.io_Command = CMD_FLUSH;
  90.    DoIO( (IO_CAST) r );                        /* flush all IO requests */
  91.    r->IOSer.io_Command = CMD_CLEAR;
  92.    DoIO( (IO_CAST) r);                        /* flush receive buffer  */
  93.  
  94.     if ( !amClosing ) {
  95.         r->IOSer.io_Command = CMD_READ;
  96.         r->IOSer.io_Length  = 1;
  97.         BeginIO( (IO_CAST) r );                /* start receive request */
  98.     }
  99. }
  100.  
  101.  
  102. /*------------------------------------------------------------*/
  103. /*  PushSerState: save current io flags                       */
  104. /*------------------------------------------------------------*/
  105. void PushSerState()
  106. {
  107.    register struct IOExtSer *r = Read_Request; 
  108.  
  109.    if ( stackSize < MAXSTACK )
  110.      {
  111.         /* Save the current Mode */
  112.         modeStack[ stackSize ] = Mode;
  113.         
  114.     /* Get the current flags */
  115.     AbortIO( (IO_CAST) r );
  116.     flagStack[ stackSize++ ] = r->io_SerFlags;
  117.     BeginIO( (IO_CAST) r );
  118.      }
  119. }
  120.  
  121. /*------------------------------------------------------------*/
  122. /*  PullSerState: restore last saved flag state              */
  123. /*------------------------------------------------------------*/
  124. void PullSerState()
  125. {
  126.    register struct IOExtSer *r = Read_Request; 
  127.  
  128.    if ( stackSize > 0 )
  129.      {
  130.     /* Reset the Mode */
  131.     Mode = modeStack[ --stackSize ];
  132.  
  133.     /* Set the old flags */
  134.     AbortIO( (IO_CAST) r );
  135.     r->io_SerFlags = flagStack[ stackSize ];
  136.     r->IOSer.io_Command = SDCMD_SETPARAMS;
  137.     DoIO( (IO_CAST) r );
  138.     r->IOSer.io_Command = CMD_READ;
  139.     BeginIO( (IO_CAST) r );
  140.      }
  141. }
  142. /*-------------------------------------------------------------*/
  143. /*           CloseSerialIO: Close the serial port              */
  144. /*-------------------------------------------------------------*/
  145. void CloseSerialIO()
  146. {
  147.    register struct IOExtSer *r = Read_Request;
  148.    register struct IOExtSer *w = Write_Request;
  149.  
  150.     if ( r != NULL ) {
  151.         amClosing = TRUE;
  152.         CloseDevice( r );
  153.         DeletePort( r->IOSer.io_Message.mn_ReplyPort );
  154.         FreeMem( r, (long)sizeof( struct IOExtSer ) );
  155.         Read_Request = NULL;
  156.     }
  157.     if ( w != NULL ) {
  158. #ifndef FJE
  159.         CloseDevice( w );
  160. #endif
  161.         DeletePort( w->IOSer.io_Message.mn_ReplyPort );
  162.         FreeMem( w, (long)sizeof( struct IOExtSer ) );
  163.         Write_Request = NULL;
  164.     }
  165. }
  166.  
  167. /*-------------------------------------------------------------*/
  168. /* InitSerialIO: Open serial IO - return read Port           */
  169. /*-------------------------------------------------------------*/    
  170. struct IOExtSer *InitSerialIO()
  171. {
  172.    register struct IOExtSer *r, *w;
  173.  
  174.     if ( Read_Request != NULL ) {
  175.         fprintf( stderr, "Error: Serial Port already open for read.\n");
  176.         return NULL;
  177.     }
  178.    r = (struct IOExtSer *) AllocMem( (long)sizeof(struct IOExtSer),
  179.      (long)(MEMF_PUBLIC|MEMF_CLEAR));
  180.    if (r == NULL)
  181.     return NULL;
  182.  
  183.    Read_Request = r;
  184.    r->io_SerFlags = SERFLAGS;
  185.    r->io_CtlChar  = CTLCHAR;
  186.    r->IOSer.io_Message.mn_ReplyPort = CreatePort("Read_RS",NULL);
  187.     if(OpenDevice(SERIALNAME,NULL,r,NULL)) {
  188.         fprintf( stderr, "Can't open Read device\n");
  189.         goto q4;
  190.     } else
  191.           printmsg( 20, "Opened read device '%s'\n", SERIALNAME);
  192.  
  193.    r->io_SerFlags = SERFLAGS;
  194.    r->io_CtlChar  = CTLCHAR;
  195.    r->io_Baud = STARTBAUD;
  196.    r->io_ReadLen = 8;
  197.    r->io_WriteLen = 8;
  198.    r->IOSer.io_Command = SDCMD_SETPARAMS;
  199.    DoIO((IO_CAST) r);
  200.    r->IOSer.io_Command = CMD_READ;
  201.    r->IOSer.io_Length = 1;
  202.    r->IOSer.io_Data = (APTR)&rs_in[0];
  203.  
  204.  
  205.    if ( Write_Request != NULL ) {
  206.     fprintf( stderr, "Error: Serial Port already open for writing.\n");
  207.     goto q3;
  208.       }
  209.    w = (struct IOExtSer *)AllocMem( (long)sizeof( struct IOExtSer ),
  210.       (long)MEMF_PUBLIC|MEMF_CLEAR);
  211.    if (w == NULL) goto q3;
  212.  
  213.    Write_Request = w;
  214.    w->io_SerFlags = SERFLAGS;
  215.    w->io_CtlChar  = CTLCHAR;
  216.    w->IOSer.io_Message.mn_ReplyPort = CreatePort("Write_RS",NULL);
  217.    if(OpenDevice(SERIALNAME,NULL,w,NULL)) {
  218.         fprintf( stderr, "Can't open Write device\n");
  219.         goto q1;
  220.     } else
  221.           printmsg( 20, "Opened write device '%s'\n", SERIALNAME);
  222.    w->io_SerFlags = SERFLAGS;
  223.    w->io_CtlChar  = CTLCHAR;
  224.    w->io_Baud = STARTBAUD;
  225.    w->io_ReadLen = 8;
  226.    w->io_WriteLen = 8;
  227.    w->IOSer.io_Command = SDCMD_SETPARAMS;
  228.    DoIO((IO_CAST) w);
  229.  
  230.    w->IOSer.io_Command = CMD_WRITE;
  231.    w->IOSer.io_Length = 1;
  232.    w->IOSer.io_Data = (APTR)&rs_out[0];
  233.  
  234.    BeginIO( (IO_CAST) r );
  235.    stackSize = 0;
  236.  
  237.    return r;
  238.  
  239. q1:   DeletePort( w->IOSer.io_Message.mn_ReplyPort );
  240. q2:   FreeMem( w, (long)sizeof( *w) );
  241. q3:   CloseDevice( r );
  242. q4:   DeletePort( r->IOSer.io_Message.mn_ReplyPort );
  243. q5:   FreeMem( r, (long)sizeof( *r) );
  244.  
  245.    return NULL;
  246. }
  247.  
  248. /*----------------------------------------------------------*/
  249. /*         SetXonMode: set Xon On or Off                    */
  250. /*----------------------------------------------------------*/
  251.  
  252. void SetXonMode( status )
  253. BOOL status;
  254. {
  255.  
  256.    register UBYTE flags;
  257.    register struct IOExtSer *r = Read_Request; 
  258.  
  259.    /* Get the current flags */
  260.    AbortIO( (IO_CAST) r );
  261.    flags = r->io_SerFlags;
  262.  
  263.    if ( status )
  264.       flags &= ~(SERF_XDISABLED);
  265.    else
  266.       flags |= SERF_XDISABLED; 
  267.    r->io_SerFlags = flags;
  268.    r->IOSer.io_Command = SDCMD_SETPARAMS;
  269.    DoIO( (IO_CAST) r );
  270.    r->IOSer.io_Command = CMD_READ;
  271.    BeginIO( (IO_CAST) r );
  272. }
  273.  
  274. /*----------------------------------------------------------*/
  275. /*         SetSerBaud: set Serial Baud Rate                 */
  276. /*----------------------------------------------------------*/
  277.  
  278. void SetSerBaud( baud )
  279. int baud;
  280. {
  281.    register struct IOExtSer *r = Read_Request; 
  282.  
  283.    /* Get the current flags */
  284.    AbortIO( (IO_CAST) r );
  285.    r->io_Baud = baud;
  286.    r->IOSer.io_Command = SDCMD_SETPARAMS;
  287.    DoIO( (IO_CAST) r );
  288.    r->IOSer.io_Command = CMD_READ;
  289.    BeginIO( (IO_CAST) r );
  290. }
  291.  
  292. /*----------------------------------------------------------*/
  293. /*         SetSerMode: set the Serial Mode            */
  294. /*----------------------------------------------------------*/
  295.  
  296. void SetSerMode( mode )
  297. int mode;
  298. {
  299.    Mode = mode;
  300. }
  301.  
  302. void SerIOBreak(count)
  303. long count;
  304. {
  305.     Write_Request->IOSer.io_Command = SDCMD_BREAK;
  306.     Write_Request->io_BrkTime = count;
  307.     DoIO( Write_Request );
  308. }
  309.  
  310. /*----------------------------------------------------------*/
  311. /*   SerIOWrite: Write a byte out the serial port (no echo) */
  312. /*----------------------------------------------------------*/
  313.  
  314. void SerIOWrite( c )
  315. register UBYTE c;
  316. {
  317.     register int err;
  318.  
  319.     *rs_out = c;
  320.     if (err = DoIO( (IO_CAST) Write_Request ))
  321.     printmsg( 0, "\nerror %d during write", err);
  322. }
  323.  
  324. /*----------------------------------------------------------*/
  325. /* SerIORead: read a byte from the serial port (no echo)    */
  326. /*----------------------------------------------------------*/
  327.  
  328. UBYTE SerIORead()
  329. {
  330.     register struct IOExtSer *r = Read_Request;
  331.     register UBYTE c;
  332.  
  333.     WaitIO( (IO_CAST) r );
  334.     c = *rs_in;
  335.     BeginIO( (IO_CAST) r );
  336.     return c;
  337. }
  338.